/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-10-31 14:24:09   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zx.cloud.domain.ClientContact;
import com.zx.cloud.persistence.ClientContactMapper;
import com.zx.cloud.persistence.CrudMapper;
import com.zx.cloud.security.Base64;
/**
 * 
 */
@Service
@Transactional
public class ClientContactService extends CrudServiceImpl<ClientContact>{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientContactService.class);
    
    @Autowired
    private ClientContactMapper clientContactMapper;
    
  	@Autowired
    public ClientContactService(CrudMapper<ClientContact> mapper) {
        super(mapper);
    }

  	public List<ClientContact> getMsgByClientId(String clientId){
  		List<ClientContact> backData = new ArrayList<ClientContact>();
  		List<ClientContact> resultData = clientContactMapper.findContactByClientId(clientId);
  		if(null != resultData && resultData.size() > 0){
  			for(ClientContact ins : resultData){
  				String contacts = ins.getContacts();
  				if(StringUtils.isNotEmpty(contacts)){
  					byte[] bytes = Base64.decode(contacts);//解密
  					String contactsData=new String(bytes);
  					JSONArray jsonArray = new JSONArray(contactsData);
  		  			for(int i = 0; i < jsonArray.length(); i ++){
  		  				JSONObject jsonObject = jsonArray.getJSONObject(i);
  		  				if(null != jsonObject){
  		  					ClientContact insContact = new ClientContact();
  		  	  				insContact.setClientId(ins.getClientId());
  		  	  				insContact.setContacts(jsonObject.getString("contacts"));
  		  	  				insContact.setContactsPhone(jsonObject.getString("contactsPhone"));
  		  	  				try {
  		  	  					String  count=jsonObject.getString("count");
  		  	  				    insContact.setCount(Integer.parseInt(count));
							} catch (Exception e) {
								insContact.setCount(0);
							}
  		  	  				insContact.setCreateTime(ins.getCreateTime());
  		  	  				
  		  	  				backData.add(insContact);
  		  				}
  		  			}
  				}
  			}
  		}
  		
  		return backData;
  	}
  	/**
  	 * @Description:获取会员通讯信息
  	 * 通讯录、通话记录、紧急联系人、短信记录
  	 */
  	public Map<String, Object>  getClientContacts(String clientId){
  		Map<String, Object> resultMap=new HashMap<String, Object>();
  		//会员通讯信息
  		List<ClientContact> resultData = clientContactMapper.findContactByClientId(clientId);
  		System.out.println("=============会员通讯信息==="+resultData.get(0).toString()+"===============");
  		/**通讯录*/
  		try {
  			byte[] contactsBytes = Base64.decode(resultData.get(0).getContacts());//解密
  			String contactsData=new String(contactsBytes);
  			resultMap.put("contacts",contactsData );
  			
  			byte[] contactsCallBytes = Base64.decode(resultData.get(0).getContactsCall());//解密
  			String contactsCallData=new String(contactsCallBytes);
  	  		resultMap.put("contactsCall", contactsCallData);
  	  		
  	     	byte[] contactsUrgentBytes = Base64.decode(resultData.get(0).getContactsUrgent());//解密
			String contactsUrgentData=new String(contactsUrgentBytes);
  	  		resultMap.put("contactsUrgent", contactsUrgentData);
  	  		
  	    	byte[] contactsSmsBytes = Base64.decode(resultData.get(0).getContactsSms());//解密
		    String contactsSmsData=new String(contactsSmsBytes);
  	  		resultMap.put("contactsSms", contactsSmsData);
		} catch (Exception e) {
			
		}
  		
  		return resultMap;
  	}
  	
  	
  	public static void main(String[] args) {
  		byte[] bytes = Base64.decode("W3siY29udGFjdHMiOiLpkonpkonkuJPlsZ7pob7pl64iLCJjb250YWN0c1Bob25lIjoiMDU3MTU2MjE1ODg4In0seyJjb250YWN0cyI6ImVoaGUiLCJjb250YWN0c1Bob25lIjoiMTA1NDgwMTg2NiJ9LHsiY29udGFjdHMiOiLpkonpkonlrpjmlrnnn63kv6EiLCJjb250YWN0c1Bob25lIjoiMTA2NTUwNTkxMTMxNDQifSx7ImNvbn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nRhY3RzUGhvbmUiOiI0MTgxODkxNiJ9LHsiY29udGFjdHMiOiJ2c2JzIiwiY29udGFjdHNQaG9uZSI6IjUxODQ1ODE4In0seyJjb250YWN0cyI6ImJzYiIsImNvbnRhY3RzUGhvbmUiOiI1MTg4ODAwMCJ9LHsiY29udGFjdHMiOiJod2hodyIsImNvbnRhY3RzUGhvbmUiOiI2NDY4NDY0OCJ9LHsiY29udGFjdHMiOiJoZWpoZCIsImNvbnRhY3RzUGhvbmUiOiI2NjQ5NDk5OSJ9LHsiY29udGFjdHMiOiJiZ2giLCJjb250YWN0c1Bob25lIjoiODU1NDg2MjEifV0=");//解密
			String contactsData=new String(bytes);
			JSONArray jsonArray = new JSONArray(contactsData);
			System.out.println("contactsData=========="+contactsData);
			System.out.println("contactsData=========="+jsonArray.length());
			
			
			int max=2;
	        int min=0;
	        Random random = new Random();
	        int s = random.nextInt(max)%(max-min+1) + min;
	        System.out.println(s);
	}

}
